#!/bin/bash
set -eo pipefail

host="$(hostname --ip-address || echo '127.0.0.1')"
if health="$(curl -fsSkL "https://$ELASTIC_USERNAME:$ELASTIC_PASSWORD@$host:5601/api/status" | python -c "import sys, json; print json.load(sys.stdin)['status']['overall']['state']")"; then
	health="$(echo "$health" | sed -r 's/^[[:space:]]+|[[:space:]]+$//g')" # trim whitespace (otherwise we'll have "green ")
	if [ "$health" = 'green' ]; then
		exit 0
	fi
	echo >&2 "unexpected health status: $health"
fi
exit 1